# -*- coding: utf-8 -*-
from datetime import datetime
from datetime import date
from time import strftime
from time import strptime
from datetime import timedelta
def getCurDate():
    """Return value of the date"""
    return date(datetime.now().year,datetime.now().month,datetime.now().day)

def getCurTime():
    """Return value of the datetime"""
    return datetime.now()
    
def converDateTimeToStr(cdate,format='%Y-%m-%d %H:%M:%S'):
    """
    Convert datetime to String
    cdata parameter must be the datetime or date of
    Return value of the date string format(%Y-%m-%d)
    """
    sdate = None
    try:
        sdate = cdate.strftime(format)
    except:
        raise ValueError
    return sdate

def converDateToDateTime(fdate):
    """
    Convert date to datetime
    fdate parameter must be the date of
    Return value of the datetime
    """
    return datetime(fdate.year,fdate.month,fdate.day,0,0,0)

def converDateTimeToDate(fdate):
    """
    Convert datetime to date
    fdate parameter must be the datetime of
    Return value of the date
    """
    return date(fdate.year,fdate.month,fdate.day)

def converStrToDate(cstr):
    """
    Convert str to date
    cstr parameter must be the str of
    Return value of the date
    """
    fdate = None
    try:
        fdate = date(*strptime(cstr, '%Y-%m-%d')[0:3])
    except:
        raise ValueError
    return fdate

def converStrToDateTime(cstr):
    """
    Convert str to datetime
    cstr parameter must be the str of
    Return value of the datetime
    """
    fdate = None
    try:
        fdate = datetime(*strptime(cstr,'%Y-%m-%d %H:%M:%S')[0:6])
    except:
        raise ValueError
    return fdate

def getTheMonthDays(year,month):
    """the number of days a month"""
    if month + 1 > 12:
        month = 1
        year = year + 1
    else:
        month = month +1
    
    return (datetime(year,month,1) + timedelta(days=-1)).day

"""
#1.获得当前日期
curdate = getCurDate()
print '1当前日期 :%s'%(curdate)

#2.获得当前时间
curtime = getCurTime()
print '2当前时间  :%s'%(curtime)

#3.将当前日期转换为字符串
print '3当前日期 :%s'%(converDateTimeToStr(curdate,'%Y-%m-%d'))

#4.将当前时间转换为字符串
print '4当前时间:%s'%(converDateTimeToStr(curtime))

#5.将当前日期转换为时间
print '5当前时间:%s' % (converDateToDateTime(curdate))

#6.将当前时间转换为日期
print '6当前日期:%s' % (converDateTimeToDate(curtime))

#7.将字符串转换为日期
print '日期:%s' % (converStrToDate('2009-04-30'))

#8.将字符串转换为时间
print '时间:%s' % (converStrToDateTime('2009-04-30 23:59:59'))

#9.获得本月有多少天
print getTheMonthDays(curdate.year,curdate.month)
"""



